package com.qf.event.listener;

/*
 *  @author Administrator
 *  @version V1.0
 *  @Project smartdeviceplatgorm
 *  @Package com.qf.event
 *  @Description
 *  @Date2022/3/17 0017 1:07
 */

import com.qf.bean.SysLogininfor;
import com.qf.bean.SysUserOnline;
import com.qf.enums.LoginResultEnum;
import com.qf.event.MyLoginEvent;
import com.qf.mapper.SysLogininforMapper;
import com.qf.mapper.SysUserOnlineMapper;
import com.qf.utils.RequestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
//@EnableAsync//开启异步支持,  @Async才会有效,这个注解只需要写一次
@Component
public class LoginEventListener {
    private SysLogininforMapper sysLogininforMapper;
    private SysUserOnlineMapper sysUserOnlineMapper;
    @Autowired
    public void setSysLogininforMapper(SysLogininforMapper sysLogininforMapper) {
        this.sysLogininforMapper = sysLogininforMapper;
    }
    @Autowired
    public void setSysUserOnlineMapperl(SysUserOnlineMapper sysUserOnlineMapper) {
        this.sysUserOnlineMapper = sysUserOnlineMapper;
    }

    Logger logger = LoggerFactory.getLogger(LoginEventListener.class);
    @EventListener
    // @Async//异步执行
    public void onEvent(MyLoginEvent event) {
        logger.error("收到登录操作的事件了");
        //我们现在只有登录的结果和用户名,还差和请求相关的所有的数据, ip 区域,操作系统等等
        //这些数据都可以从请求中获取
        //请求对象,我们发现我们少了一request对象

        //因为是通过threadlocal获取的数据,所以拿的时候不能放到子线程
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        CompletableFuture.runAsync(()->{
            HttpServletRequest request = requestAttributes.getRequest();
            String id = request.getSession().getId();//获取sessionId
            String ip = RequestUtil.getRemoteHost(request);//获取ip
            Map<String, String> map = RequestUtil.getOsAndBrowserInfo(request);//获取操作系统和浏览器信息
            String locationByIp = RequestUtil.getLocationByIp(ip);//获取ip的对应地区
            //保存数据到对象中
            SysLogininfor sysLogininfor=new SysLogininfor();
            sysLogininfor.setIpaddr(ip);
            sysLogininfor.setLoginLocation(locationByIp);
            sysLogininfor.setLoginName(event.getUserName());
            sysLogininfor.setLoginTime(new Date());
            sysLogininfor.setBrowser(map.get("browser"));
            sysLogininfor.setOs(map.get("os"));
            switch (event.getLoginResultEnum()) {
                case FAIL:
                    sysLogininfor.setMsg("登录失败");
                    sysLogininfor.setStatus("0");
                    break;
                case SUCCESS:
                    sysLogininfor.setMsg("登录成功");
                    sysLogininfor.setStatus("1");
                    break;
            }
            //保存到数据库

            sysLogininforMapper.addLoginLog(sysLogininfor);
            if (event.getLoginResultEnum() == LoginResultEnum.SUCCESS) {
                //在登录成功之后还需要更新下在线用户
                //在线用户的数据中我们只有一个数据不知道, sessionid,我们已经拿到request,只需要通过它拿到session id
                SysUserOnline sysUserOnline=new SysUserOnline();
                BeanUtils.copyProperties(sysLogininfor,sysUserOnline);
                sysUserOnline.setSessionId(id);
                sysUserOnline.setExpireTime(30);
                sysUserOnline.setStartTimestamp(new Date());
                logger.error("在线用户的数据对象"+sysUserOnline);
                int count = sysUserOnlineMapper.findByUserName(sysUserOnline.getLoginName());
                if (count == 1) {
                    sysUserOnlineMapper.updateSysUserOnline(sysUserOnline);
                }else {
                    sysUserOnlineMapper.addSysUserOnline(sysUserOnline);
                }

            }
        });
    }
}
